<?php

namespace App\HttpController\Index;

use EasySwoole\Template\Render;
use App\HttpController\BaseController;
use App\Model\User;
use EasySwoole\VerifyCode\Conf;
use EasySwoole\EasySwoole\Task\TaskManager;
use App\HttpController\Index\Task as IndexTask;

class Index extends BaseController
{

	//首页 模板加载分配变量
    function index()
    {
    
    	$data = ['name' => 'easySwoole'];
        $this->fetch('index/index',$data);
    }
    
    
   //异步任务投递 
   function task(){
   		$request = $this->request();
    	$param = $request->getRequestParam();//获取post/get数据,get覆盖post
		
		//复杂逻辑使用模板任务投递
   		TaskManager::getInstance()->async(new IndexTask($param));
   			
   		//简单使用,不能使用外部对象资源
	   	TaskManager::getInstance()->async(function (){
	   		sleep(5);
		    var_dump('task 11111');
		    echo "\r\n";
		});
			
   }
   
   //定时器
   function timer(){
   	
   		// 每隔 10 秒执行一次
		$timerId = \EasySwoole\Component\Timer::getInstance()->loop(10 * 1000, function () {
		    echo "this timer runs at intervals of 10 seconds\n";
		});
		
		
		// 10 秒后执行一次
		$timerId2 =	\EasySwoole\Component\Timer::getInstance()->after(10 * 1000, function () {
		    echo "ten seconds later\n";
		});
		
		//清除定时器
		//   \EasySwoole\Component\Timer::getInstance()->clear($timerId);
		//   \EasySwoole\Component\Timer::getInstance()->clear($timerId2);
		
   }
   


	//orm操作
    function user(){
    	$request = $this->request();
    	$param = $request->getRequestParam();//获取post/get数据,get覆盖post
		
		$UserModel = new User;
		$UserModel->test();
	
    	$this->writeJson(200,$param,'success');
    }
    
    //验证码图片
    function vcode(){
    	$config = new Conf();
        $code = new \EasySwoole\VerifyCode\VerifyCode($config);
        $this->response()->withHeader('Content-Type','image/png');
        $this->response()->write($code->DrawCode()->getImageByte());
    }
    
    //验证码内容
    function CaptchaCode(){
        $config = new Conf();
        $code = new \EasySwoole\VerifyCode\VerifyCode($config);
        $this->response()->write($code->DrawCode()->getImageCode());
    }
    
    //jwt使用
    function jwt(){
    	$jwt = \EasySwoole\Jwt\Jwt::getInstance();
    	$obj = $jwt->algMethod('AES')->setSecretKey('测试呀')->publish();

		$obj->setExp(time() + 3600);
		$obj->setData('测试数据a');
		
		$token = $obj->__toString();
	
		try {
		  /** @var \EasySwoole\Jwt\JwtObject $result */
		  $result = $jwt->decode($toke.'fsdafafn');
		
		  switch ($result->getStatus()) {
		    case -1:
		      echo 'token无效';
		    case  1:
		      echo '验证通过';
		      break;
		    case  2:
		      echo '验证失败';
		      break;
		    case  3:
		      echo 'token过期';
		      break;
		  }
		    
		  // 根据解密之后的结果完善业务逻辑
		 // var_dump($result);
		  
		} catch (\Exception $e) {
		  // TODO: 处理异常
		  echo "error";
		}

		$this->response()->write($token);
    }
    
    
    //redis 缓存
    function redis(){
    	
    	go(function (){
    	    $instance = \EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS');
		    $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig([
		        'host' => $instance['host'],
		        'port' => $instance['port'],
		        'auth' => $instance['auth'],
		        'serialize' => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE
		    ]));
		    //var_dump($redis);
			var_dump($redis->set('a','aaaa'));
    		var_dump($redis->get('a'));
		});
    }
    

	//重新渲染模板
	function reload(){
        Render::getInstance()->restartWorker();
        $this->response()->write('Reload Template');
    }
    
    protected function actionNotFound(?string $action)
    {
        $this->response()->withStatus(404);
        $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
        if(!is_file($file)){
            $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
        }
        $this->response()->write(file_get_contents($file));
    }
}